//
//  KCMainViewController.swift
//  S0002_NavigationController
//
//  Created by yao_yu on 14-9-1.
//  Copyright (c) 2014年 yao_yu. All rights reserved.
//

import UIKit

protocol KCMainDelegate{
    func showUserInfoWithUserName(userName:String)
}

class KCMainViewController: UIViewController, KCMainDelegate,UIActionSheetDelegate {

    var loginInfo = UILabel()
    var loginButton = UIBarButtonItem()
    var meButton = UIBarButtonItem()
    var isLogon = false
    
    override func viewDidLoad() {
        super.viewDidLoad()

        addNavigationBar()
        addLoginInfo()
        
    }
    
    func addLoginInfo(){
        loginInfo.frame = CGRectMake(0, 100, 320, 30)
        loginInfo.textAlignment = NSTextAlignment.Center
        view.addSubview(loginInfo)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // 添加导航栏
    func addNavigationBar(){
        let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, 320, 44+20))
//        navigationBar.tintColor = UIColor.whiteColor()
        view.addSubview(navigationBar)
        
        // 创建导航控件内容
        var pnavigationItem = UINavigationItem(title: "网络聊天")
        
        // 左侧登录
        loginButton = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Done, target: self, action: "login")
        pnavigationItem.leftBarButtonItem = loginButton
        
        // 右侧关于
        meButton = UIBarButtonItem(title: "我", style: UIBarButtonItemStyle.Done, target: self, action: "showinfo")
        meButton.enabled = true
        pnavigationItem.rightBarButtonItem = meButton
        
        // 添加内容到导航栏
        navigationBar.pushNavigationItem(pnavigationItem, animated: false)
    }
    
    func login(){
        if !isLogon{
            let loginController  = KCLoginViewController()
            loginController.delegate = self
            presentViewController(loginController, animated: false, completion: nil)
        } else {
            var actionSheet = UIActionSheet(title: "系统消息", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "注销")
            actionSheet.showInView(self.view)
        }
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    
    func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int) {
        if buttonIndex == 0{
            isLogon = false
            loginButton.title = "登录"
            loginInfo.text = ""
            meButton.enabled = false
        }
    }
    
    func showinfo(){
        let meController = KCMeViewController()
        meController.userInfo = loginInfo.text!
        presentViewController(meController, animated: true, completion: nil)
    }
    
    func showUserInfoWithUserName(userName: String) {
        isLogon = true
        loginInfo.text = "你好, \(userName)"
        loginButton.title = "注销"
        meButton.enabled = true
    }

}
